package top.truism.stack;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;

/**
 * 用一个栈实现另一个栈的排序
 */
public class StackSort {

    /**
     * 实现对 @param{stack} 排序,元素从栈顶到栈底排序为从大到小
     *
     * @param stack
     */
    public static void sortStack(Stack<Comparable> stack) {

        Deque<Comparable> helpStack = new ArrayDeque<>();
        while (!stack.isEmpty()) {
            Comparable cur = stack.pop();
            while (!helpStack.isEmpty() && helpStack.peek().compareTo(cur) < 0) {
                stack.push(helpStack.pop());
            }
            helpStack.push(cur);
        }
        while (!helpStack.isEmpty()) {
            stack.push(helpStack.pop());
        }
    }


}
